﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Ext.Net;
using System.Data;
using Model;
using System.Configuration;

namespace TCDC_System.Content
{
    public partial class Default : System.Web.UI.Page
    {
        private DBUtil DB;
        private UserInnerJoinCampaign objUserInnerJoinCampaign;

        UserInnerJoinCampaign i = new UserInnerJoinCampaign();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.DB = new DBUtil();
            DB.strConn = ConfigurationManager.AppSettings["ConnString"];

            objUserInnerJoinCampaign = new UserInnerJoinCampaign();
            objUserInnerJoinCampaign.strConn = ConfigurationManager.AppSettings["ConnString"];

  

            this.CreateNode();

            //Console
            if (!X.IsAjaxRequest)
            {
                GetFavorites();
                ShowCounterService();
            }
            //ShowCounterService();

            //Console

        }

        protected void RefreshTime(object sender, DirectEventArgs e)
        {
            
            GridPanelLastRegister.Title = "กรอกข้อมูลสมัครสมาชิกล่าสุด " + DateTime.Now.ToString("HH:mm:ss");
            i.status="pending";
            DataTable dt = objUserInnerJoinCampaign.getByObject(i);
            if (dt != null && dt.Rows.Count != Convert.ToInt32(LastRegister.Text))
            {
                LastRegister.Text = dt.Rows.Count.ToString();
                StoreUserInnerJoinCampaign.DataSource = dt;
                StoreUserInnerJoinCampaign.DataBind();
            }

            /*** Status Time ***/
            clock.Text = DateTime.Now.ToString("HH:mm:ss");

        }

        protected void rowSelectLastRegister(object sender, DirectEventArgs e)
        {
            string user_campaign_id = e.ExtraParams["select_user_campaign_id"];
            Ext.Net.X.Js.AddScript("loadPageLastRegis(#{Pages}, " + user_campaign_id + ");");
        }


        protected void ShowCounterService()
        {
            //Notification.Show(new NotificationConfig
            //{
            //    Title = "จำนวนผู้ใช้บริการ",
            //    AutoHide = false,
            //    Width = 250,
            //    Height = 150,
            //    AlignCfg = new NotificationAlignConfig
            //    {
            //        ElementAnchor =  AnchorPoint.BottomRight,
            //        TargetAnchor = AnchorPoint.BottomRight,
            //        OffsetX = -1,
            //        OffsetY = -130
            //    },

            //    Icon = Icon.Information,
            //    Html = "<br>ขณะนี้มีผู้ใช้บริการ 200 คน <br><br> วันนี้มีการใช้บัตร 1 day pass : 11 คน <br><br> วันนี้มีการใช้บัตร Tourist pass : 4 คน"
            //});
        }


        protected void SetCollapsed(object sender, DirectEventArgs e)
        {
            Panel2.Collapsed = true;
        }

        protected void AddFavorites(object sender, DirectEventArgs e)
        {
            FavoritesAdd1.Show();
        }
        //static node creation with children
        private Ext.Net.TreeNode CreateNode(SiteMapNode siteMapNode)
        {
            Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();

            if (!string.IsNullOrEmpty(siteMapNode.Url))
            {
                treeNode.Href = this.Page.ResolveUrl(siteMapNode.Url);
            }

            treeNode.NodeID = siteMapNode.Key;
            treeNode.Text = siteMapNode.Title;
            treeNode.Qtip = siteMapNode.Description;
  
            SiteMapNodeCollection children = siteMapNode.ChildNodes;

            if (children != null && children.Count > 0)
            {
                foreach (SiteMapNode mapNode in siteMapNode.ChildNodes)
                {
                    treeNode.Nodes.Add(this.CreateNode(mapNode));
                }
            }

            return treeNode;
        }

        private void CreateNode()
        {
            Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();

            string sql = "SELECT * FROM tcdc_mst_function " +
                         "WHERE parent_id = '0' ORDER BY sorting_no ";

            DataTable dt = DB.ExecuteReader(sql);

            treeNode.NodeID = "root";
            treeNode.Text = "TCDC System";
            treeNode.Qtip = "TCDC System";

            foreach (DataRow row in dt.Rows)
            {

                treeNode = CreateChildNodes(row, treeNode);

            }


            TreePanel1.Root.Add(treeNode);
        }

        private Ext.Net.TreeNode CreateChildNodes(DataRow row, Ext.Net.TreeNode treeNode)
        {
            Ext.Net.TreeNode childNode = new Ext.Net.TreeNode();


            childNode.NodeID = row["function_id"].ToString();
            childNode.Text = row["function_name"].ToString();
            childNode.Qtip = row["function_name"].ToString();
            childNode.Href = row["url"].ToString();

            string sql = "SELECT * FROM tcdc_mst_function " +
                         "WHERE parent_id = '" + row["function_id"].ToString() +
                         "' ORDER BY sorting_no ";

            DataTable dt = DB.ExecuteReader(sql);

            foreach (DataRow r in dt.Rows)
            {
                childNode = CreateChildNodes(r, childNode);
            }

            treeNode.Nodes.Add(childNode);
            return treeNode;
        }

        private void GetFavorites()
        {
            string host = HttpContext.Current.Request.Url.Authority;

            XElement document = XElement.Load("http://" + host + "/Attribute/xml/DashboardSchema.ashx");
            var defaultIcon = document.Attribute("defaultIcon") != null ? document.Attribute("defaultIcon").Value : "";

            var query = from g in document.Elements("group")
                        select new
                        {
                            Title = g.Attribute("title") != null ? g.Attribute("title").Value : "",
                            Items = (from i in g.Elements("item")
                                     select new
                                     {
                                         Title = i.Element("title") != null ? i.Element("title").Value : "",
                                         Icon = i.Element("item-icon") != null ? i.Element("item-icon").Value : defaultIcon,
                                         Id = i.Element("id") != null ? i.Element("id").Value : "",
                                         Url = i.Element("url") != null ? i.Element("url").Value : ""
                                     }
                                )
                        };

            this.Store1.DataSource = query;
            this.Store1.DataBind();
        }
    }
}